package com.xzx.mr.mapjoin;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * 缓存小表到内存中，然后直接处理大表，还可以省略掉reduce阶段
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/7/26 22:30
 */
public class DistributedCacheDriver {
    public static void main(String[] args) throws IOException, URISyntaxException, ClassNotFoundException,
            InterruptedException {
        args = new String[]{"d:/hadoopinput/mapjoin/order.txt", "d:/output"};

        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(DistributedCacheDriver.class);

        //只有map阶段没有reduce阶段
        job.setMapperClass(DistributedCacheMapper.class);

        // 没有reduce阶段，map阶段的输出就是最终输出，所以不用设置map阶段的输出，直接设置最终输出即可
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //设置加载缓存文件的路径
        job.addCacheFile(new URI("file:///d:/hadoopinput/mapjoin/pd.txt"));

        //因为没有reduce阶段,所以设置为0
        job.setNumReduceTasks(0);

        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}
